﻿Imports VN.INS.BL.BC
Imports System.Data
Imports System.Web
Imports System.IO

Partial Class Devida_consolidado_avance_Mensual
    Inherits System.Web.UI.Page

    Dim dtcomentario As New DataTable
    Dim dtaccion As New DataTable
    Dim dtproblema As New DataTable

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        If Not (Page.IsPostBack()) Then
            ListaPPER()
            Listaperiodo()

           btnGenerar.Attributes.Add("onclick", "javascript:document.getElementById('" + btnGenerar.ClientID + "').disabled=true;" + Me.GetPostBackEventReference(btnGenerar))

        End If
    End Sub

    Public Sub ListaPPER()
        Dim objPPER As PPERBC
        Dim objData As DataTable
        objPPER = New PPERBC
        objData = New DataTable
        objData = objPPER.ListaPPER("")
        ddlPPER.DataSource = objData
        ddlPPER.DataTextField = "DESCRIPCION"
        ddlPPER.DataValueField = "PPER"
        ddlPPER.DataBind()
    End Sub

    Public Sub cargar_grilla(ByVal mes As String, ByVal año As String, ByVal pper As String)
        Dim ObjconsolidadoBC As consolidadoBC
        Dim ObjData As DataTable
        Dim ObjData1 As DataTable
        'Cargamos detalle de grilla principal
        ObjconsolidadoBC = New consolidadoBC
        ObjData = ObjconsolidadoBC.cargar_grilla(mes, año, ddlPPER.SelectedValue)
        ViewState("Data2") = ObjData
        ' 'Cargamos el tipo de transeferencia
        ObjData1 = ObjconsolidadoBC.ListarTipoAsignacion_PPER(Session("Año"), ddlPPER.SelectedValue)
        ViewState("Data1") = ObjData1
        gvconsolidado.DataSource = ObjData1
        gvconsolidado.DataBind()
    End Sub

    Public Sub cargar_grilla2()
        'Cargamos Datos de la segunda grilla
        Dim ObjconsolidadoBC As consolidadoBC
        Dim ObjData As DataTable
        Dim ObjData1 As DataTable
        Dim ObjData2 As DataTable
        Dim ObjData3 As DataTable
        Dim ObjData4 As DataTable
        Dim objData5 As DataTable
        ObjconsolidadoBC = New consolidadoBC
        ObjData = ObjconsolidadoBC.cargar_grilla2
        ObjData1 = ObjconsolidadoBC.tipo_trans2
        ObjData2 = ObjconsolidadoBC.tipo_PROY
        ObjData3 = ObjconsolidadoBC.tipo_ACTV
        ObjData4 = ObjconsolidadoBC.tipo_ACC
        objData5 = ObjconsolidadoBC.CargarTotalTipo()

        ViewState("data2") = ObjData
        ViewState("tipo_PROY") = ObjData2
        ViewState("tipo_ACTV") = ObjData3
        ViewState("tipo_ACC") = ObjData4
        ViewState("TotalTipo") = objData5
        If Not (ObjData1 Is Nothing) Then
            If ObjData1.Rows.Count > 0 Then
                gvFormato5.DataSource = ObjData1
                gvFormato5.DataBind()
            End If
        End If
    End Sub

    Public Sub cargar_grilla_coment(ByVal cod As String)
        Dim ObjconsolidadoBC As consolidadoBC
        Dim ObjData As DataTable
        'Cargamos comentarios de el informe dado
        ObjconsolidadoBC = New consolidadoBC
        ObjData = ObjconsolidadoBC.cargar_grilla_coment(cod)

        If ObjData.Rows.Count = 0 Then 'Si no hay comentarios cargamos textbox vacio
            Dim dtcomentario As New DataTable
            dtcomentario.Columns.Add("DESCRIPCION")
            dtcomentario.Columns.Add("COMENT")
            Dim rowc As DataRow = dtcomentario.NewRow()
            rowc("DESCRIPCION") = ""
            rowc("COMENT") = ""
            dtcomentario.Rows.Add(rowc)
            Session("dtcomentario") = dtcomentario
            'gv_comentario.DataSource = dtcomentario
            'gv_comentario.DataBind()
        Else 'Si hay datos entonces se muestran
            Session("dtcomentario") = ObjData
            'gv_comentario.DataSource = ObjData
            'gv_comentario.DataBind()
        End If

    End Sub

    Public Sub cargar_grilla_acc(ByVal cod As String)
        Dim ObjconsolidadoBC As consolidadoBC
        Dim ObjData As DataTable
        ObjconsolidadoBC = New consolidadoBC
        'Cargamos acciones de el informe dado
        ObjData = ObjconsolidadoBC.cargar_grilla_acc(cod)
        If ObjData.Rows.Count = 0 Then 'Si no hay acciones cargamos textbox vacio
            Dim dtaccion As New DataTable
            dtaccion.Columns.Add("DESCRIPCION")
            dtaccion.Columns.Add("COMENT")
            Dim rowA As DataRow = dtaccion.NewRow()
            rowA("DESCRIPCION") = ""
            rowA("COMENT") = ""
            dtaccion.Rows.Add(rowA)
            Session("dtaccion") = dtaccion
            'gv_acciones.DataSource = dtaccion
            'gv_acciones.DataBind()
        Else 'Si hay datos entonces se muestran
            Session("dtaccion") = ObjData
            'gv_acciones.DataSource = ObjData
            'gv_acciones.DataBind()
        End If
    End Sub

    Public Sub cargar_grilla_prob(ByVal cod As String)
        Dim ObjconsolidadoBC As consolidadoBC
        Dim ObjData As DataTable
        ObjconsolidadoBC = New consolidadoBC
        'Cargamos problemas de el informe dado
        ObjData = ObjconsolidadoBC.cargar_grilla_prob(cod)
        If ObjData.Rows.Count = 0 Then 'Si no hay problemas cargamos textbox vacio
            Dim dtproblema As New DataTable
            dtproblema.Columns.Add("DESCRIPCION")
            dtproblema.Columns.Add("COMENT")
            Dim rowp As DataRow = dtproblema.NewRow()
            rowp("DESCRIPCION") = ""
            rowp("COMENT") = ""
            dtproblema.Rows.Add(rowp)
            Session("dtproblema") = dtproblema
            'gv_problemas.DataSource = dtproblema
            'gv_problemas.DataBind()
        Else 'Si hay datos entonces se muestran
            Session("dtproblema") = ObjData
            'gv_problemas.DataSource = ObjData
            'gv_problemas.DataBind()
        End If
    End Sub

    Private Sub Listaperiodo()
        Dim objBC As GeneralBC
        Dim objDt As DataTable
        Try
            objBC = New GeneralBC
            objDt = New DataTable
            objDt = objBC.ListaMes(Session("Año"))
            ddlPeriodo.DataSource = objDt
            ddlPeriodo.DataTextField = "DESCRIPCION"
            ddlPeriodo.DataValueField = "PERIODO"
            ddlPeriodo.DataBind()
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Protected Sub gvconsolidado_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvconsolidado.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            ' Capturamos TIPO ASIGNACION RECURSO de la fila
            Dim hfPPER As HiddenField = CType(e.Row.FindControl("hfPPER"), HiddenField)
            ' Capturamos su descripcion
            If hfPPER.Value = "1" Then
                ViewState("descrip") = "TRANSFERENCIA FINANCIERA"
            End If
            If hfPPER.Value = "2" Then
                ViewState("descrip") = "ASIGNACION DIRECTA (OTRAS ENTIDADES)"
            End If
            If hfPPER.Value = "3" Then
                ViewState("descrip") = "ASIGNACION DIRECTA (DEVIDA)"
            End If
            'Filtramos el detalle por TIPO ASIGNACION RECURSO
            Dim IDpper As String = hfPPER.Value.ToString
            Dim vst As DataView = New DataView(ViewState("Data2"), "TIPO_ASIGNACION_RECURSO='" + IDpper + "'", "", DataViewRowState.CurrentRows)
            Dim gdv As GridView = e.Row.Cells(0).FindControl("gvDetalle")
            'Dim gdv As GridView = e.Row.Cells(0).FindControl("gvTipo")
            e.Row.Cells(0).HorizontalAlign = HorizontalAlign.Left
            e.Row.Cells(0).BackColor = Drawing.Color.White
            gdv.DataSource = vst
            gdv.DataBind()
        End If
    End Sub

    Public Sub RDB_Tipo(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim hfTipo As HiddenField = CType(e.Row.FindControl("hfTipo"), HiddenField)
            'Filtramos el detalle por TIPO ASIGNACION RECURSO
            Dim vst As DataView = New DataView(ViewState("Data2"), "TIPO_ASIGNACION_RECURSO='" + hfTipo.Value + "'", "", DataViewRowState.CurrentRows)
            Dim gdv As GridView = e.Row.Cells(0).FindControl("gvDetalle")
            e.Row.Cells(0).HorizontalAlign = HorizontalAlign.Left
            e.Row.Cells(0).BackColor = Drawing.Color.White
            gdv.DataSource = vst
            gdv.DataBind()
        End If
    End Sub

    'Declaramos variables de suma
    Private _total1, _total2, _total3 As Decimal
    Private _totalPresMan1, _totalPresMan2, _totalPresMan3 As Decimal
    Private _totalMonGir1, _totalMonGir2, _totalMonGir3 As Decimal
    Private _totalE1, _totalE2, _totalE3 As Decimal
    Private _totalIafg1, _totalIafg2, _totalIafg3 As Decimal
    Private n As Int16 = 0
    Public Sub RDB(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
        If e.Row.RowType = DataControlRowType.DataRow Then ' Por cada fila           
            e.Row.BackColor = Drawing.Color.White
        End If
    End Sub

    Protected Sub btn_guardar_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btn_guardar.Click
        InsertarInforme()
        'Dim i As Integer
        'Dim k As Integer
        'Dim j As Integer
        ''recorremos grilla Comentario
        'For i = 0 To gv_comentario.Rows.Count - 1
        '    Dim DESCR As TextBox = CType(gv_comentario.Rows(i).FindControl("txt_comentario"), TextBox)
        '    Dim htxtCM As HiddenField = CType(gv_comentario.Rows(i).FindControl("hfcom"), HiddenField)
        '    If htxtCM.Value <> "" Then  'Si existe Actualiza
        '        update_coment(htxtCM.Value, DESCR.Text)
        '    Else 'Si es nuevo
        '        If Len(DESCR.Text) > 0 Then 'Solo si tiene texto se inserta
        '            Dim objgen As GeneralBC
        '            objgen = New GeneralBC
        '            Dim coment As String
        '            'Generamos codigo de comentario
        '            coment = objgen.GenerarCodigoNumero("COMENT_PPER_FIN_MEN_PROGRAMA", "COMENT")
        '            'Guardamos Comentario
        '            insert_coment(coment, DESCR.Text, ViewState("COD"), "C")
        '        End If
        '    End If
        'Next
        ''recorremos grilla acciones
        'For j = 0 To gv_acciones.Rows.Count - 1
        '    Dim DESCR As TextBox = CType(gv_acciones.Rows(j).FindControl("txt_comentario"), TextBox)
        '    Dim htxtCM As HiddenField = CType(gv_acciones.Rows(j).FindControl("hfcom"), HiddenField)
        '    If htxtCM.Value <> "" Then 'Si existe Actualiza
        '        update_coment(htxtCM.Value, DESCR.Text)
        '    Else 'Si es nuevo
        '        If Len(DESCR.Text) > 0 Then 'Solo si tiene texto se inserta
        '            Dim objgen As GeneralBC
        '            objgen = New GeneralBC
        '            Dim coment As String
        '            'Generamos codigo de comentario Acciones
        '            coment = objgen.GenerarCodigoNumero("COMENT_PPER_FIN_MEN_PROGRAMA", "COMENT")
        '            'Guardamos Comentario Acciones
        '            insert_coment(coment, DESCR.Text, ViewState("COD"), "A")
        '        End If
        '    End If
        'Next
        ''recorremos grilla problemas
        'For k = 0 To gv_problemas.Rows.Count - 1
        '    Dim DESCR As TextBox = CType(gv_problemas.Rows(k).FindControl("txt_comentario"), TextBox)
        '    Dim htxtCM As HiddenField = CType(gv_problemas.Rows(k).FindControl("hfcom"), HiddenField)
        '    If htxtCM.Value <> "" Then  'Si existe Actualiza
        '        update_coment(htxtCM.Value, DESCR.Text)
        '    Else  'Si es nuevo
        '        If Len(DESCR.Text) > 0 Then 'Solo si tiene texto se inserta
        '            Dim objgen As GeneralBC
        '            objgen = New GeneralBC
        '            Dim coment As String
        '            'Generamos codigo de comentario Problema
        '            coment = objgen.GenerarCodigoNumero("COMENT_PPER_FIN_MEN_PROGRAMA", "COMENT")
        '            'Guardamos Comentario Problema
        '            insert_coment(coment, DESCR.Text, ViewState("COD"), "P")
        '        End If
        '    End If
        'Next
        alerta.Mostrar_Alerta("Aviso", "Datos Guardados correctamente")
        cargar_grilla_coment(ViewState("COD"))
        cargar_grilla_acc(ViewState("COD"))
        cargar_grilla_prob(ViewState("COD"))
    End Sub

    'Insertar comentario
    Private Sub insert_coment(ByVal COmX As String, ByVal descrip As String, ByVal id_pper_mensual As String, ByVal tipo As String)
        Dim objconsolidadoBC As consolidadoBC
        Try
            objconsolidadoBC = New consolidadoBC
            objconsolidadoBC.insert_coment(COmX, descrip, id_pper_mensual, tipo)
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    'Actualizar comentario
    Public Sub update_coment(ByVal COMX As String, ByVal descrip As String)
        Dim objconsolidadoBC As consolidadoBC
        Try
            objconsolidadoBC = New consolidadoBC
            objconsolidadoBC.update_coment(COMX, descrip)
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Private Sub InsertarInforme()
        Dim OBJGENERAL As GeneralBC
        OBJGENERAL = New GeneralBC
        Dim ID_AVANCE_pper As String
        Dim periodX As String
        Dim m As Integer
        Dim añ As String
        'Captura del mes anterior
        m = Convert.ToInt32(Now.Month) - 1
        If m = 0 Then
            añ = Convert.ToInt32(Now.Year) - 1
            m = 12
        Else
            añ = Convert.ToInt32(Now.Year)
        End If
        'Captura del año
        periodX = m.ToString + "-" + añ
        Dim fech As String
        'Obtenemos el mes
        fech = MonthName(m)
        Dim firm_M As String = ""
        Dim firm_pper As String = ""
        If ViewState("COD") = "" Then ' si es nuevo 
            'Generamos codigo
            ID_AVANCE_pper = OBJGENERAL.GenerarCodigo("SEG_PER_AVAN_FIN_MEN_PROGRAMA", "INF_C_AV", "IAV-")
            'guardamos informe
            'insert_avan_pper(ID_AVANCE_pper, periodX, Session("PPER").ToString, txtFecha.Text, firm_M, firm_pper)
            ViewState("COD") = ID_AVANCE_pper
            'txtFecha.Enabled = False
        Else 'si es edicion
            ID_AVANCE_pper = ViewState("COD")
        End If
    End Sub

    'Insertar avance
    Private Sub insert_avan_pper(ByVal idppe As String, ByVal period As String, ByVal pper As String, ByVal fech As String, ByVal firm_M As String, ByVal firm_pper As String)
        Dim objconsolidadoBC As consolidadoBC
        Try
            objconsolidadoBC = New consolidadoBC
            objconsolidadoBC.insert_avan_pper(idppe, period, pper, fech, firm_M, firm_pper)
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Protected Sub gvFormato5_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvFormato5.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim hfPPER As HiddenField = CType(e.Row.FindControl("hfPPER2"), HiddenField)
            Dim DATA As DataTable = New DataTable
            If hfPPER.Value = "1" Then
                DATA = ViewState("tipo_PROY")
                e.Row.Cells(0).BackColor = Drawing.Color.FromArgb(255, 255, 204)
            End If
            If hfPPER.Value = "2" Then
                DATA = ViewState("tipo_ACTV")
                e.Row.Cells(0).Text = "PRODUCTO"
                e.Row.Cells(0).BackColor = Drawing.Color.FromArgb(255, 255, 218)
            End If
            If hfPPER.Value = "3" Then
                e.Row.Cells(0).BackColor = Drawing.Color.FromArgb(255, 255, 232)
                DATA = ViewState("tipo_ACC")
            End If
            e.Row.Cells(0).ForeColor = Drawing.Color.FromArgb(0, 0, 0)
            Dim IDpper As String = hfPPER.Value.ToString
            Dim vst As DataView = New DataView(DATA, "TIPO_PROYECTO_ACTIVIDAD='" + IDpper + "'", "", DataViewRowState.CurrentRows)
            Dim gdv As GridView = e.Row.Cells(1).FindControl("gvDetalle2")
            ViewState("TIPO") = IDpper.ToString
            gdv.DataSource = vst
            gdv.DataBind()
        ElseIf e.Row.RowType = DataControlRowType.Header Then
            Dim lblTotalPPER As Label = CType(e.Row.FindControl("lblTotalPPER"), Label)
            Dim lblProgramadoPPER As Label = CType(e.Row.FindControl("lblProgramadoPPER"), Label)
            Dim lblTransferidoPPER As Label = CType(e.Row.FindControl("lblTransferidoPPER"), Label)
            Dim lblGiradoPPER As Label = CType(e.Row.FindControl("lblGiradoPPER"), Label)
            Dim lblPorcentajePPER As Label = CType(e.Row.FindControl("lblPorcentajePPER"), Label)
            Dim DT As DataTable
            DT = New DataTable
            DT = ViewState("TotalTipo")
            lblTotalPPER.Text = Convert.ToDecimal(DT.Rows(0).Item("TOTAL") + DT.Rows(1).Item("TOTAL") + DT.Rows(2).Item("TOTAL")).ToString("#,##0.00")
            lblProgramadoPPER.Text = Convert.ToDecimal(DT.Rows(0).Item("MONTO_PROGRAMADO") + DT.Rows(1).Item("MONTO_PROGRAMADO") + DT.Rows(2).Item("MONTO_PROGRAMADO")).ToString("#,##0.00")
            lblTransferidoPPER.Text = Convert.ToDecimal(DT.Rows(0).Item("MONTO_TRANSFERIDO") + DT.Rows(1).Item("MONTO_TRANSFERIDO") + DT.Rows(2).Item("MONTO_TRANSFERIDO")).ToString("#,##0.00")
            lblGiradoPPER.Text = Convert.ToDecimal(DT.Rows(0).Item("MONTO_GIRADO") + DT.Rows(1).Item("MONTO_GIRADO") + DT.Rows(2).Item("MONTO_GIRADO")).ToString("#,##0.00")
            lblPorcentajePPER.Text = (Convert.ToDecimal(lblGiradoPPER.Text) / Convert.ToDecimal(lblProgramadoPPER.Text)).ToString("0 %")
        End If
    End Sub

    Public Sub RDB2(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim hfCOD As HiddenField = CType(e.Row.FindControl("hfCOD"), HiddenField)
            Dim Hftip As HiddenField = CType(e.Row.FindControl("Hftip"), HiddenField)
            Dim lblProducto As Label = CType(e.Row.FindControl("lblPPER2"), Label)
            Dim A As String
            Dim DATA As New DataTable
            If Hftip.Value = "1" Then
                DATA = ViewState("data2")
                e.Row.Cells(0).BackColor = Drawing.Color.FromArgb(255, 255, 218)
                A = "P"
            End If
            If Hftip.Value = "2" Then
                DATA = ViewState("data2")
                e.Row.Cells(0).BackColor = Drawing.Color.FromArgb(255, 255, 218)
                A = "A"
            End If
            If Hftip.Value = "3" Then
                e.Row.Cells(0).BackColor = Drawing.Color.FromArgb(255, 255, 218)
                DATA = ViewState("data2")
                A = "AC"
            End If
            With e.Row.Cells(0)
                .ForeColor = Drawing.Color.FromArgb(0, 0, 0)
                .HorizontalAlign = HorizontalAlign.Center
                .Text.ToUpper()
            End With
            ViewState("Producto") = lblProducto.Text
            Dim vst As DataView
            Dim Hftip2 As String = Hftip.Value.ToString
            vst = New DataView(DATA, "TIPO_PROYECTO_ACTIVIDAD='" + Hftip2 + "' AND TIPO_PROYECTO_DES = '" + hfCOD.Value.ToString + "'", "", DataViewRowState.CurrentRows)
            ViewState("TOTAL") = 0
            ViewState("MONTO_PROGRAMADO") = 0
            ViewState("MONTO_TRANSFERIDO") = 0
            ViewState("MONTO_GIRADO") = 0

            Dim dt As New DataTable
            dt = vst.ToTable

            For i As Int16 = 0 To dt.Rows.Count - 1
                ViewState("TOTAL") = ViewState("TOTAL") + dt.Rows(i)("Total")
                ViewState("MONTO_PROGRAMADO") = ViewState("MONTO_PROGRAMADO") + dt.Rows(i)("MONTO_PROGRAMADO")
                ViewState("MONTO_TRANSFERIDO") = ViewState("MONTO_TRANSFERIDO") + dt.Rows(i)("MONTO_TRANSFERIDO")
                ViewState("MONTO_GIRADO") = ViewState("MONTO_GIRADO") + dt.Rows(i)("MONTO_GIRADO")
            Next
            Dim gdv As GridView = e.Row.Cells(1).FindControl("gvDetalle3")
            gdv.DataSource = vst
            gdv.DataBind()
        ElseIf e.Row.RowType = DataControlRowType.Header Then
            e.Row.Cells(0).ColumnSpan = 2
            e.Row.Cells(0).ForeColor = Drawing.Color.Black
            e.Row.Cells(1).Visible = False
            Dim lblTitulo As Label = CType(e.Row.FindControl("lblTitulo"), Label)
            Dim lblTotal As Label = CType(e.Row.FindControl("lblTotal"), Label)
            Dim lblProgramado As Label = CType(e.Row.FindControl("lblProgramado"), Label)
            Dim lblTransferido As Label = CType(e.Row.FindControl("lblTransferido"), Label)
            Dim lblGirado As Label = CType(e.Row.FindControl("lblGirado"), Label)
            Dim lblPorcentaje As Label = CType(e.Row.FindControl("lblPorcentaje"), Label)
            Dim vst As DataView
            vst = New DataView(ViewState("TotalTipo"), "TIPO_PROYECTO_ACTIVIDAD='" + ViewState("TIPO").ToString + "'", "", DataViewRowState.CurrentRows)
            lblTotal.Text = Convert.ToDecimal(vst.ToTable().Rows(0).Item("TOTAL")).ToString("#,##0.00")
            lblProgramado.Text = Convert.ToDecimal(vst.ToTable().Rows(0).Item("MONTO_PROGRAMADO")).ToString("#,##0.00")
            lblTransferido.Text = Convert.ToDecimal(vst.ToTable().Rows(0).Item("MONTO_TRANSFERIDO")).ToString("#,##0.00")
            lblGirado.Text = Convert.ToDecimal(vst.ToTable().Rows(0).Item("MONTO_GIRADO")).ToString("#,##0.00")
            lblPorcentaje.Text = Convert.ToDecimal(vst.ToTable().Rows(0).Item("PORCENT")).ToString("0 %")
            Select Case ViewState("TIPO")
                Case "1"
                    lblTitulo.Text = "TOTAL PROYECTOS"
                Case "2"
                    lblTitulo.Text = "TOTAL PRODUCTOS"
                Case "3"
                    lblTitulo.Text = "TOTAL ACCIONES COMUNES"
            End Select
        End If
    End Sub

    Private _total, _prog, _transf, _girado As Decimal
    Public Sub RDB3(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
        _total = ViewState("TOTAL")
        _prog = ViewState("MONTO_PROGRAMADO")
        _transf = ViewState("MONTO_TRANSFERIDO")
        _girado = ViewState("MONTO_GIRADO")
        If e.Row.RowType = DataControlRowType.Header Then
            e.Row.Cells(0).ColumnSpan = 2
            e.Row.Cells(8).Visible = False
            e.Row.Cells(0).Text = "Total " + ViewState("Producto").ToString
            For i As Int16 = 0 To 7
                If i = 0 Then
                    e.Row.Cells(i).BackColor = Drawing.Color.FromArgb(200, 200, 200)
                Else
                    e.Row.Cells(i).BackColor = Drawing.Color.FromArgb(255, 255, 255)
                    e.Row.Cells(i).Style("text-align") = "Right"
                    e.Row.Cells(i).Style("font-size") = "9px"
                    e.Row.Cells(i).Style(" font-family") = "Arial"
                    e.Row.Cells(i).Font.Bold = True
                End If
                e.Row.Cells(i).ForeColor = Drawing.Color.FromArgb(0, 0, 0)
                e.Row.Cells(i).Font.Bold = False
                e.Row.Cells(i).BorderColor = Drawing.Color.FromArgb(117, 163, 208)
            Next
            e.Row.Cells(1).Text = Me._total.ToString("#,##0.00")
            e.Row.Cells(2).Text = Me._prog.ToString("#,##0.00")
            e.Row.Cells(3).Text = Me._transf.ToString("#,##0.00")
            e.Row.Cells(4).Text = Me._girado.ToString("#,##0.00")
            If Me._prog = 0 Then
                e.Row.Cells(5).Text = (0).ToString("0 %")
            Else
                e.Row.Cells(5).Text = (Me._girado / Me._prog).ToString("0 %")
            End If
            e.Row.Cells(6).Text = ""
            e.Row.Cells(7).Text = ""
        ElseIf e.Row.RowType = DataControlRowType.DataRow Then
            e.Row.BackColor = Drawing.Color.FromArgb(255, 255, 255)
        End If
    End Sub

    'Salir
    Protected Sub btnCerrar_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnCerrar.Click
        Response.Redirect("Lista_Consolidado.aspx")
    End Sub

    Protected Sub btnWord_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnWord.Click
        ExportWord()
    End Sub

    'Exportar a word
    Protected Sub ExportWord()
        Dim Cabecera As String =
                                 "<h5 style='text-align:center; ; font-family:calibri'>INFORME CONSOLIDADO MENSUAL DE AVANCE DEL PROGRAMA</h5> " + _
                                 "<div style='width: 680px; text-align:center'>" + _
                                 "<table style='width: 680px; '>" + _
                                 "<tr style='text-align:left;'><td style='width: 200px; font-size:9pt; font-weight:bold; '>Informe correspondiente al mes de : </td><td style='font-size:8pt'>" + ddlPeriodo.SelectedItem.Text + "</td></tr>" + _
                                 "<tr><td style='font-size:9pt; font-weight:bold; '>Fecha de elaboración : </td><td style='font-size:8pt'>" + Server.HtmlDecode(ddlPeriodo.SelectedItem.Text) + "</td></tr>" + _
                                 "</table>" + _
                                 "</div><br>" + _
                                 "<div style='width: 680px; text-align:center'>" + _
                                 "<table style='width: 680px; '>" + _
                                 "<tr style='text-align:left; font-family:calibri'><td style='width: 10px;'><h5>I.-</h5></td><td colspan='2' ><h5>" + Server.HtmlDecode("NIVEL DE AVANCE GENERAL DEL PPER") + "</h5></td></tr>" + _
                                 "</table>" + _
                                 "</div>"

        Dim ChartTable1 As String = "<div style='width: 680px; text-align:center'>" + _
                                    "<table style='width: 680px; padding: 0; margin: 0; text-align: center; font-size: 8pt'  cellpadding='0' cellspacing='0'>" + _
                                    "<tr style='padding: 0; margin: 0; background-color: #CCCCCC' >" + _
                                    "<td style='width: 195px; border-style: solid ; border-color: #123456; border-width: 1px' rowspan='2'>Modalidad de Ejec.</td>" + _
                                    "<td colspan='2' style='border-style: solid solid none none; border-color: #123456; border-width: 1px'>Monto</td>" + _
                                    "<td style='width: 90px; border-style: solid solid solid none; border-color: #123456; border-width: 1px' rowspan='2'>Monto Transferido</td>" + _
                                    "<td style='border-style: solid solid none none; border-color: #123456; border-width: 1px' colspan='2'>Ejecucion Financiera</td>" + _
                                    "<td style='width: 96px; border-style: solid solid solid none; border-color: #123456; border-width: 1px' rowspan='2'>Ejecucion</td>" + _
                                    "<td style='border-style: solid solid solid none; border-color: #123456; border-width: 1px' rowspan='2'>Financiera</td>" + _
                                    "</tr>" + _
                                    "<td style='width: 91px; background-color: #CCCCCC; border-style: solid solid solid none; border-color: #123456; border-width: 1px;'>Total S/. </td>" + _
                                    "<td style='width: 93px; background-color: #CCCCCC; border-style: solid solid solid none; border-color: #123456; border-width: 1px;'>Programado Acumulado (1)</td>" + _
                                    "<td style='width: 93px; background-color: #CCCCCC; border-style: solid solid solid none; border-color: #123456; border-width: 1px;'>Monto S/. (2)</td>" + _
                                    "<td style='width: 93px; background-color: #CCCCCC; border-style: solid solid solid none; border-color: #123456; border-width: 1px;'>%</td>" + _
                                    "<tr>" + _
                                    "</tr>"
        For a As Integer = 1 To 3
            If a = "1" Then
                ViewState("a") = "TRANSFERENCIA FINANCIERA"
            End If
            If a = "2" Then
                ViewState("a") = "ASIGNACION DIRECTA (OTRAS ENTIDADES)"
            End If
            If a = "3" Then
                ViewState("a") = "ASIGNACION DIRECTA (DEVIDA)"
            End If
            ChartTable1 = ChartTable1 + "<tr style='height:15px'><td colspan='8' style='font-size: 6pt; border-style: solid solid solid solid; border-width: 1px; border-color: #123456; text-align:left'>" + ViewState("a") + "</td></tr>"

            Dim dtFisico As DataTable
            dtFisico = New DataTable
            dtFisico = ViewState("Data2")
            Dim vst As DataView
            vst = New DataView(dtFisico, "TIPO_ASIGNACION_RECURSO='" & a & "'", _
                              "", DataViewRowState.CurrentRows)
            Dim porc1 As String = "Hola"
            Dim Total1, Total2, Total3, Total4, Total5 As Double
            Total1 = 0
            Total2 = 0
            Total3 = 0
            Total4 = 0
            Total5 = 0
            For i As Int16 = 0 To vst.ToTable.Rows.Count - 1
                'If vst.ToTable.Rows(i).Item("DESCRIPCION").ToString = "" Then
                '    porc1 = ""
                'Else
                '    porc1 = vst.ToTable.Rows(i).Item("DESCRIPCION").ToString
                'End If
                ChartTable1 = ChartTable1 + "<tr style='font-size: 6pt; height:15px'>" + _
                                            "<td style='border-style: solid solid solid solid; border-width: 1px; border-color: #123456; text-align:left' >" + vst.ToTable.Rows(i).Item("TIPO_PROY_ACT_DESC").ToString + "</td>" + _
                                            "<td style='border-style: solid solid solid none; border-width: 1px; border-color: #123456; text-align:right' >" + vst.ToTable.Rows(i).Item("TOTAL").ToString + "</td>" + _
                                            "<td style='border-style: solid solid solid none; border-width: 1px; border-color: #123456; text-align:right' >" + vst.ToTable.Rows(i).Item("ACUMULADO").ToString + "</td>" + _
                                            "<td style='border-style: solid solid solid none; border-width: 1px; border-color: #123456; text-align:right' >" + vst.ToTable.Rows(i).Item("MONTO_TRANSFERIDO").ToString + "</td>" + _
                                            "<td style='border-style: solid solid solid none; border-width: 1px; border-color: #123456; text-align:right' >" + vst.ToTable.Rows(i).Item("EJECUTADO").ToString + "</td>" + _
                                            "<td style='border-style: solid solid solid none; border-width: 1px; border-color: #123456; text-align:right' >" + CDbl(vst.ToTable.Rows(i).Item("PORC")).ToString("P0") + "</td>" +
                                            "<td style='border-style: solid solid solid none; border-width: 1px; border-color: #123456; text-align:right'></td>" + _
                                            "<td style='border-style: solid solid solid none; border-width: 1px; border-color: #123456; text-align:left' > </td></tr>"
                Total1 = Total1 + vst.ToTable.Rows(i).Item("TOTAL")
                Total2 = Total2 + vst.ToTable.Rows(i).Item("ACUMULADO")
                Total3 = Total3 + vst.ToTable.Rows(i).Item("MONTO_TRANSFERIDO")
                Total4 = Total4 + vst.ToTable.Rows(i).Item("EJECUTADO")
            Next
            Dim TotalPorc As Double = Total4 / IIf(Total2 = 0, 1, Total2)
            ChartTable1 = ChartTable1 + "<tr style='font-size: 6pt; height:15px'>" + _
                                            "<td style='border-style: solid solid solid solid; border-width: 1px; border-color: #123456; text-align:left' >Sub Total</td>" + _
                                            "<td style='border-style: solid solid solid none; border-width: 1px; border-color: #123456; text-align:right' >" + Total1.ToString + "</td>" + _
                                            "<td style='border-style: solid solid solid none; border-width: 1px; border-color: #123456; text-align:right' >" + Total2.ToString + "</td>" + _
                                            "<td style='border-style: solid solid solid none; border-width: 1px; border-color: #123456; text-align:right' >" + Total3.ToString + "</td>" + _
                                            "<td style='border-style: solid solid solid none; border-width: 1px; border-color: #123456; text-align:right' >" + Total4.ToString + "</td>" + _
                                            "<td style='border-style: solid solid solid none; border-width: 1px; border-color: #123456; text-align:right' >" + TotalPorc.ToString("P0") + "</td>" + _
                                            "<td style='border-style: solid solid solid none; border-width: 1px; border-color: #123456; text-align:right'></td>" + _
                                            "<td style='border-style: solid solid solid none; border-width: 1px; border-color: #123456; text-align:left' > </td></tr>"
        Next
        ChartTable1 = ChartTable1 + "</table></div>"

        Dim dtFinanciero As DataTable
        dtFinanciero = New DataTable
        dtFinanciero = Session("dtcomentario")
        Dim sum1, sum2, sum3, sum4, sum5, sum6, sum7 As Double
        sum1 = 0
        sum2 = 0
        sum3 = 0
        sum4 = 0
        sum5 = 0
        sum6 = 0
        sum7 = 0

        Dim strComentarioFis As String = "<div style='width: 680px; text-align:center'>" + _
                                    "<table style='border-width: 1px 1px 1px 1px; width: 680px;  " + _
                                    "color: #000000; ' cellpadding='4' cellspacing='0'> " + _
                                    "<tr style='font-weight: bold; font-size: 7.5pt; font-family:Calibri'><td style='width:25px;'><h5>II.-</h5></td><td><h5>COMENTARIOS RELEVANTES DERIVADOS DEL AVANCE </h5></td></tr>"
        Dim dtComFis As DataTable
        dtComFis = New DataTable
        dtComFis = Session("dtcomentario")
        For j As Int16 = 0 To dtComFis.Rows.Count - 1
            strComentarioFis = strComentarioFis + "<tr style='vertical-align:top; font-size:8pt; height:30px'><td>2." + (j + 1).ToString + "</td><td><PRE style='font-size:8pt; font-family:Times New Roman'>" + dtComFis.Rows(j).Item("DESCRIPCION").ToString + "</PRE></td></tr>"
        Next
        strComentarioFis = strComentarioFis + "</table></div>"

        Dim strComentarioFin As String = "<div style='width: 680px; text-align:center'>" + _
                                            "<table style='border-width: 0; width: 680px;' >" + _
                                            "<tr style='font-weight: bold; font-size:8pt; font-family:Calibri'><td style='width:25px;'><h5>III.-</h5></td><td><h5>PROBLEMAS O LIMITANTES</h5></td></tr>"
        Dim dtComFin As DataTable
        dtComFin = New DataTable
        dtComFin = Session("dtcomentario")
        For k As Int16 = 0 To dtComFis.Rows.Count - 1
            strComentarioFin = strComentarioFin + "<tr style='vertical-align:top; font-size:8pt; height:30px'><td style='width:25px;'>3." + (k + 1).ToString + "</td><td><PRE style='font-size:8pt; font-family:Times New Roman'>" + dtComFin.Rows(k).Item("DESCRIPCION").ToString + "</PRE></td></tr>"
        Next
        strComentarioFin = strComentarioFin + "</table></div>"

        Dim strObservaciones As String = "<div style='width: 680px; text-align:center'>" + _
                                   "<table style='border-width: 1px 1px 1px 1px; width: 680px; " + _
                                   "color: #000000; ' cellpadding='4' cellspacing='0'> " + _
                                   "<tr style='font-weight: bold; font-size:7.5pt; font-family:Calibri'><td style='width:25px;'><h5>IV.-</h5></td><td><h5>ACCIONES ADOPTADAS</h5></td></tr>"
        Dim dtAccion As DataTable
        dtAccion = New DataTable
        dtAccion = Session("dtaccion")
        For l As Int16 = 0 To dtAccion.Rows.Count - 1
            strObservaciones = strObservaciones + "<tr style='vertical-align:top; font-size:8pt; height:30px'><td style='width:25px;'>4." + (l + 1).ToString + "</td><td><PRE style='font-size:8pt; font-family:Times New Roman'>" + dtAccion.Rows(l).Item("DESCRIPCION").ToString + "</PRE></td></tr>"
        Next
        strObservaciones = strObservaciones + "</table></div>"

        Dim strFirma As String = "<div style='width: 220px; text-align:center'>" + _
                                   "<table style='border-width: 1px 1px 1px 1px; width: 220px; " + _
                                   "color: #000000; font-family: Calibri' cellpadding='4' cellspacing='0'> " + _
                                   "<tr style='font-weight: bold; font-size:9pt;'><td style='text-align:center; width:220px; border-top: solid 1px #000000;'>Firma</td></tr>" + _
                                   "<tr style='font-weight: bold; font-size:9pt;'><td style='text-align:center; width:220px;'>Responsable del PPER</td></tr>"
        strFirma = strFirma + "</table></div><br>"

        Dim strIndice As String = "<div style='width: 680px; text-align:center'>" + _
                                   "<table style='border-width: 1px 1px 1px 1px; border-style:solid; border-color:black; width: 240px; " + _
                                   "color: #000000; ' cellpadding='0' cellspacing='0'> " + _
                                   "<tr style='font-weight: bold; font-size:7.5pt;'><td style='text-align:center; width:120px; border-right:1px solid black'>Indice del Avance Físico Global (IAFG)</td><td style='text-align:center; width:120px;'></td></tr>" + _
                                   "</table></div>"
        Response.Clear()
        Response.Buffer = True
        Response.ContentType = "application/vnd.ms-word"
        Response.AddHeader("Content-Disposition", "attachment;filename=GridViewExport.doc")
        Response.Charset = "UTF-8"
        Response.ContentEncoding = Encoding.Default
        Dim sb As StringBuilder = New StringBuilder()
        Dim sw As StringWriter = New StringWriter(sb)
        Dim htw As HtmlTextWriter = New HtmlTextWriter(sw)
        Dim pagina As Page = New Page
        Dim form = New HtmlForm

        pagina.EnableEventValidation = False
        pagina.DesignerInitialize()
        pagina.Controls.Add(form)
        pagina.RenderControl(htw)

        Response.Output.Write(Cabecera.ToString)
        Response.Output.Write("<br>")
        Response.Output.Write(ChartTable1.ToString)
        Response.Output.WriteLine("<br>")
        Response.Output.Write(strComentarioFis.ToString)
        Response.Output.WriteLine("<br>")
        Response.Output.Write(strComentarioFin.ToString)
        Response.Output.WriteLine("<br>")
        Response.Output.Write(strObservaciones.ToString)
        Response.Output.WriteLine("<br><br><br><br><br><br><br><br>")
        Response.Output.WriteLine(strFirma)
        Response.Flush()
        Response.End()
    End Sub

    Protected Sub btnGenerar_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnGenerar.Click
        System.Threading.Thread.Sleep(5000)
        'Dim m As String
        'Dim Y As Integer
        'Dim fech As String
        ''Cargamos grilla Comentario con textbox vacio
        'Dim dtcomentario As New DataTable
        'dtcomentario.Columns.Add("DESCRIPCION")
        'dtcomentario.Columns.Add("COMENT")
        'Dim rowc As DataRow = dtcomentario.NewRow()
        'rowc("DESCRIPCION") = ""
        'rowc("COMENT") = ""
        'dtcomentario.Rows.Add(rowc)
        'Session("dtcomentario") = dtcomentario
        ''Cargamos grilla Accion con textbox vacio
        'Dim dtaccion As New DataTable
        'dtaccion.Columns.Add("DESCRIPCION")
        'dtaccion.Columns.Add("COMENT")
        'Dim rowA As DataRow = dtaccion.NewRow()
        'rowA("DESCRIPCION") = ""
        'rowA("COMENT") = ""
        'dtaccion.Rows.Add(rowA)
        'Session("dtaccion") = dtaccion
        ''Cargamos grilla Problemas con textbox vacio
        'Dim dtproblema As New DataTable
        'dtproblema.Columns.Add("DESCRIPCION")
        'dtproblema.Columns.Add("COMENT")
        'Dim rowp As DataRow = dtproblema.NewRow()
        'rowp("DESCRIPCION") = ""
        'rowp("COMENT") = ""
        'dtproblema.Rows.Add(rowp)
        'Session("dtproblema") = dtproblema
        ''Capturamos el mes anterior al actual y su año
        'm = ddlPeriodo.SelectedValue.ToString.Substring(4, 2) 'Convert.ToInt32(Now.Month) - 1
        'If m = 0 Then
        '    Y = Convert.ToInt32(Now.Year) - 1
        '    m = 12
        'Else
        '    Y = Convert.ToInt32(Now.Year)
        'End If
        ''Mostramos mes y año en etiqueta
        'fech = (MonthName(m) + " de " + Y.ToString).ToUpper

        ''Cargamos grilla de primera pestaña
        'cargar_grilla(m, Y, Session("PPER").ToString)
        ''Cargamos grilla de segunda pestaña
        'cargar_grilla2()
        lblPrueba.Text = "ok " + Now.ToLongTimeString
        'divReporte.Style("display") = "block"
        alerta.Mostrar_Alerta("AA", "BB")
    End Sub

End Class